<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>药品页面</title>
<link rel="stylesheet" href="../../../layui/css/layui.css" media="all">
</head>
<body>
	<div class="demoTable">
	  搜索:
	  <div class="layui-inline">
	    <input class="layui-input" style="width: 150px;" name="drugName" id="drugName" autocomplete="off" placeholder="输入药品关键字">
	  </div>
	  <div class="layui-inline layui-form" style="width: 150px;">
			<select name="dtId" id="dtId"
				autocomplete="off" class="layui-input">
				<option></option>
			</select>
	  </div>
	  <div class="layui-inline">
	    <input class="layui-input" style="width: 150px;" name="drugBarCode" id="drugBarCode" autocomplete="off" placeholder="输入药品编码">
	  </div>
	  <div class="layui-inline">
	    <input class="layui-input" style="width: 130px;" name="drugPinyin" id="drugPinyin" autocomplete="off" placeholder="输入药品简称">
	  </div>
      <div class="layui-input-inline" style="width: 100px;">
        <input type="number" id="priceMin" name="priceMin" onkeyup="priceMin()" placeholder="￥" autocomplete="off" class="layui-input">
      </div>
      -
      <div class="layui-input-inline" style="width: 100px;">
        <input type="number" id="priceMax" name="priceMax" onkeyup="priceMax()"   placeholder="￥" autocomplete="off" class="layui-input">
      </div>
	  <button class="layui-btn" data-type="reload">搜索</button>
	  <button class="layui-btn" id="drugAdd" data-type="add">增加</button>
	</div>
	
	<table class="layui-hide" id="DrugModel" lay-filter="demo"></table>
 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs " lay-event="edit" id="imgUpdate">修改</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  <a class="layui-btn layui-btn-normal  layui-btn-xs" lay-event="smx" id="smx">说明书</a>
  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="pzcl">配置材料</a>
</script>
 
 <script type="text/javascript" src="../../../res/js/base.js"></script>
<script src="../../../layui/layui.js"></script>
<script type="text/javascript" src="../../../res/wzw/js/jquery.min.1.6.4.js"></script>
<script src="../../../res/wzw/js/jquery-1.10.2.js"></script>
<script>

layui.use(['form','laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element
	  ,form=layui.form;//元素操作
	 
	  //监听Tab切换
	  element.on('tab(demo)', function(data){
	    layer.msg('切换了：'+ this.innerHTML);
	    console.log(data);
	  });
	  
	  //执行一个 table 实例
	  table.render({
	     elem: '#DrugModel'
	    ,cellMinWidth: 80
	    ,url: getRootPath()+'DrugModel/findAll.action' //数据接口
	    ,page: true //开启分页
	    ,method:'post'
	    ,cols: [[ //表头
	       {type:'checkbox'}
	      /* ,{field: 'drugId', title: '药品ID', width:200} */
	      ,{field: 'dtName', title: '药品类型', width:100}
	      ,{field: 'drugBarCode', title: '药品编码', width:150}
	      ,{field: 'drugName', title: '药品名称', width:150}
	      ,{field: 'pov', title: '药品单位', width:100}
	      ,{field: 'drugPrice', title: '分店价格', width:100}
	      ,{field: 'speId', title: '批发价格', width:100}
	      ,{field: 'drugPrices', title: '建议零售价', width:100}
	      ,{field: 'drugPinyin', title: '药品拼音简称', width:180}
	      ,{field:'drugImg',title:'药品图片', width:120, templet:'<div><img src="{{ d.drugImg}}"></div>'}
	      ,{fixed: 'right',title: '操作', width: 300, align:'center', toolbar: '#barDemo'}
	    ]]
	  	,id: 'testReload'
	  });
	  
	  //监听工具条
	  table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    var data = obj.data //获得当前行数据
	    ,layEvent = obj.event; //获得 lay-event 对应的值
	    if(layEvent === 'del'){
	      var drugId=data.drugId;
	      layer.confirm('确认删除？', function(index){
	        /* obj.del(); */ //删除对应行（tr）的DOM结构
	        layer.close(index);
	        //使用jquery的ajax方式向后台提交数据
			var url = "../../../DrugModel/delete.action";
			var data={drugId:drugId};
				$.post(url,data,function(mes){
					if(mes.msg=="ok"){
						layer.msg('删除成功', {
							  icon: 1,
							  time: 1000 //1秒关闭（如果不配置，默认是3秒）
							}, function(){
								 location.reload(); // 页面刷新
						});
					}
					if(mes.msg=="nos"){
						layer.msg('已有药品库存,无法删除', {
							  icon: 1,
							  time: 1000 //1秒关闭（如果不配置，默认是3秒）
							}, function(){
								 location.reload(); // 页面刷新
						});
					}
					if(mes.msg=="no"){
						layer.msg('已有药品生产计划,无法删除', {
							  icon: 1,
							  time: 1000 //1秒关闭（如果不配置，默认是3秒）
							}, function(){
								 location.reload(); // 页面刷新
						});
					}
				});
	      	});
	    } else if(layEvent === 'edit'){
	    	var drugId=data.drugId;

	        //使用jquery的ajax方式向后台提交数据
			var url = "../../../DrugModel/findById.action";
			var data={drugId:drugId};
			$.post(url,data,function(data){
				 layer.open({
					  type: 2,
					  area : ['85%' , '90%'],//弹出框大小
					  shift:1,//弹出框动画效果
					  content: ['drugUpdate.html','on'], //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
				 	  success:function(layero,index){
				 		  var body = layer.getChildFrame('body',index);//确定两个页面之间的父子关系
				 		  var iframeWin = window[layero.find('iframe')[0]['name']];//子页面对象
				 		  console.log(body.html()) //得到iframe页的body内容
				 		  body.find("#drugId").val(data.drugId);
				 		  body.find("#pids").val(data.dtId);
				 		  body.find("#drugName").val(data.drugName);
				 		  body.find("#drugPrice").val(data.drugPrice);
				 		  body.find("#pov").val(data.pov);
				 		  body.find("#speId").val(data.speId);
				 		  body.find("#drugPrices").val(data.drugPrices);
				 		  body.find("#drugPinyin").val(data.drugPinyin);
				 		  body.find("#drugImg").val(data.drugImg);
				 	  }
				}); 	
				   
			},dataType="JSON");
	    } else if(layEvent=="smx"){
	    	var drugId=data.drugId;
	    	//alert(drugId);
	        //使用jquery的ajax方式向后台提交数据
			var url = "../../../specification/findByDrugId.action";
			var data={drugId:drugId};
			$.post(url,data,function(data){
				layer.open({
					  type: 2,
					  area : ['90%' , '95%'],//弹出框大小
					  shift:1,//弹出框动画效果
					  content: ['specificationUpdate.html','on'], //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
				 	  success:function(layero,index){
				 		  var body = layer.getChildFrame('body',index);//确定两个页面之间的父子关系
				 		  var iframeWin = window[layero.find('iframe')[0]['name']];//子页面对象
				 		  console.log(body.html()) //得到iframe页的body内容
				 		  body.find("#drugId").val(drugId);
				 		  body.find("#speId").val(data.speId);
				 		  body.find("#speName").val(data.speName);
				 		  body.find("#speComponent").val(data.speComponent);
				 		  body.find("#speCharacter").val(data.speCharacter);
				 		  body.find("#speType").val(data.speType);
				 		  body.find("#speIndications").val(data.speIndications);
				 		  body.find("#speStandards").val(data.speStandards);
				 		  body.find("#speUsage").val(data.speUsage);
				 		  body.find("#speUntowardEffect").val(data.speUntowardEffect);
				 		  body.find("#speTaboo").val(data.speTaboo);
				 		  body.find("#speMna").val(data.speMna);
				 		  body.find("#speInteractions").val(data.speInteractions);
				 		  body.find("#speEffect").val(data.speEffect);
				 		  body.find("#speStorage").val(data.speStorage);
				 		  body.find("#spePack").val(data.spePack);
				 		  body.find("#speIndate").val(data.speIndate);
				 		  body.find("#speStandard").val(data.speStandard);
				 		  body.find("#speApn").val(data.speApn);
				 	  }
				}); 	
				   
			},dataType="JSON");
	    }else if(layEvent=="pzcl"){
	    	var drugId=data.drugId;
	    	layer.open({
				  type: 	2,
				  area : ['100%' , '100%'],//弹出框大小
				  shift:1,//弹出框动画效果
				  content: 'drugMaterials.html?drugId='+drugId //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
			}); 
	    }
	  });
	  
	  //条件查询
	  var $ = layui.$;
			  
	  $('.demoTable .layui-btn').on('click', function(){
		 var drugName = $('#drugName');
		 var dtId = $('#dtId');
		 var drugBarCode = $('#drugBarCode');
		 var drugPinyin = $('#drugPinyin');
		 var priceMin = $('#priceMin');
		 var priceMax = $('#priceMax');
		 //执行重载
	     table.reload('testReload', {
	       page: {
	         curr: 1 //重新从第 1 页开始
	       }
	       ,where: {
	    	   drugName: drugName.val(),
	    	   dtId:dtId.val(),
	    	   drugBarCode:drugBarCode.val(),
	    	   drugPinyin:drugPinyin.val(),
	    	   priceMin:priceMin.val(),
	    	   priceMax:priceMax.val()
	       }
	     });
	  });
	  $('#drugAdd').on('click', function(){
		 layer.open({
			  type: 	2,
			  area : ['90%' , '93%'],//弹出框大小
			  shift:1,//弹出框动画效果
			  content: ['drugAdd.html','on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		 }); 
	   });
	  var url = "../../../DrugModel/findAllType.action";
		$.post(url,null,function(data){
			for(var i=0;i<data.length;i++){
				$("#dtId").append("<option value="+data[i].dtId+">"+data[i].dtName+"</option>");
			}
			//form表单重新渲染
			form.render();
		},dataType="JSON");
		form.render('select','selFilter'); 
	 /*  function reloadData(){
		  location.reload(); // 页面刷新
	  } */
	}); 
function priceMin(){
	var priceMin =Number(document.getElementById('priceMin').value);
	if(priceMin<=0||priceMin>=1000){
       $("#priceMin").attr("placeholder","1-999￥");
       document.getElementById('priceMin').value="";
       return false;
	}
}
function priceMax(){
	var priceMax =Number(document.getElementById('priceMax').value);
	var priceMin =Number(document.getElementById('priceMin').value);
	
	if(priceMax<=0||priceMax>=1000){
       $("#priceMax").attr("placeholder","1-999￥");
       document.getElementById('priceMax').value="";
       return false;
	}
	if(priceMax<=priceMin){
	   $("#priceMax").attr("placeholder","价格不符");
       document.getElementById('priceMax').value="";
       return false;
	}
}
</script>
</body>
</html>